package online.misscl.lsy.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import online.misscl.lsy.controller.BaseController;
import online.misscl.lsy.entity.HospitalInformation;
import online.misscl.lsy.entity.VaccineSortHospital;
import online.misscl.lsy.entity.dto.VaccineSortQueryDTO;
import online.misscl.lsy.entity.dto.VaccineSortUpdateDTO;
import online.misscl.lsy.entity.dto.vo.TableDataInfoVo;
import online.misscl.lsy.entity.dto.vo.VaccineSortVo;
import online.misscl.lsy.service.VaccineSortService;
import online.misscl.lsy.service.impl.HospitalInformationServiceImpl;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * project name: IntelliJ IDEA
 * Date: 2023/3/11 13:01
 *
 * @PackageName: online.misscl.lsy.controller.back
 * @ClassName: VaccineSortController
 * @Author: code mover
 * @Version:
 * @Description:
 */
@Api(tags = "疫苗分类管理")
@RestController
@RequestMapping("/back")
public class BackVaccineSortController extends BaseController {

    @Autowired
    private VaccineSortService vaccineSortService;

    @Autowired
    private HospitalInformationServiceImpl hospitalInformationService;


    @ApiOperation("REST风格的分页查询")
    @GetMapping("/vaccineSorts/{pageSize}/{pageNum}")
    public AjaxResult list(@PathVariable Integer pageSize,@PathVariable Integer pageNum){
        return AjaxResult.success(vaccineSortService.vaccineSortList(new VaccineSortQueryDTO(pageNum,pageSize)));
    }

    @ApiOperation("内嵌pageSize的条件查询")
    @GetMapping("/vaccineSorts")
    public TableDataInfoVo<List<VaccineSortVo>> queryList(VaccineSortQueryDTO vaccineSortQueryDTO){
        List<VaccineSortVo> list = vaccineSortService.vaccineSortQueryList(vaccineSortQueryDTO);
        Page page = PageUtil.getPages(vaccineSortQueryDTO.getPageNum(),vaccineSortQueryDTO.getPageSize(),list);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @ApiOperation("获取单个分类")
    @GetMapping("/vaccineSort/{vaccineSortId}")
    public AjaxResult getOne(@PathVariable Integer vaccineSortId){
        return AjaxResult.success(vaccineSortService.getOneVaccineSortVo(vaccineSortId));
    }


    @ApiOperation("模糊搜索医院名称")
    @GetMapping("/vaccineSortsVague")
    public TableDataInfoVo<List<VaccineSortVo>> vagueList(String key){
        LambdaQueryWrapper<HospitalInformation> qw = new LambdaQueryWrapper<HospitalInformation>();
        qw.select(HospitalInformation::getHospitalName,HospitalInformation::getHospitalId)
                .like(HospitalInformation::getHospitalName,key);
        List<HospitalInformation> list = hospitalInformationService.list(qw);;
        Page page = PageUtil.getPages(1,10,list);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/vaccineSort/{vaccineSortId}")
    public AjaxResult deleteVaccineSort(@PathVariable Integer vaccineSortId){
//        LambdaQueryWrapper
        return toAjax(vaccineSortService.deleteVaccineSort(vaccineSortId));
    }
    @ApiOperation("修改分类")
    @PutMapping("/vaccineSorts")
    public AjaxResult updateVaccineSort(@RequestBody VaccineSortUpdateDTO vaccineSortUpdateDTO){
//        获取字典表中对应id
        Integer vaccineSortId = vaccineSortService.getVaccineSortId(vaccineSortUpdateDTO);
//        修改关联表
        if(vaccineSortId!=0)
            return toAjax(vaccineSortService.updateVaccineSort(
                    new VaccineSortHospital(
                    vaccineSortUpdateDTO.getId()
                    ,vaccineSortId
                    ,vaccineSortUpdateDTO.getHospitalId())));
        else{
            return AjaxResult.error("分类名已存在，修改取消");
        }

    }


    @ApiOperation("新增分类")
    @PostMapping("/vaccineSorts")
    public AjaxResult saveVaccineSort(@RequestBody VaccineSortUpdateDTO vaccineSortUpdateDTO){
//        将获取分类字典中的id查出并添加在关联表中
         Integer vaccineSortId = vaccineSortService.getVaccineSortId(vaccineSortUpdateDTO);
        if(vaccineSortId!=0)
            return toAjax(vaccineSortService.saveVaccineSort(new VaccineSortHospital(vaccineSortId,vaccineSortUpdateDTO.getHospitalId())));
        else{
            return AjaxResult.error("分类名已存在，新增取消");
        }
    }


}
